using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public enum Element_T
{
    T_UINT8=1,
    T_UINT16=2,
    T_UINT32=3,
    T_FLOAT=4,
    T_INT16=5,
    T_INT32=6,
}
public struct ELEMENT_STRUCT
{
    public byte size;
    public byte type;
    public uint data;
    public ELEMENT_STRUCT(Element_T type,uint data)
    {
        
        this.size = dll_api.type_get_size((byte)type);
        this.type = (byte)type;
        this.data = data;
    }
}

public class dll_api : MonoBehaviour
{
    // Start is called before the first frame update
    
    [DllImport("libhluart.dylib")]
    public static extern byte type_get_size(byte type);

    [DllImport("libhluart.dylib")]
    public static extern void read_buffer(byte[] buffer, uint length, ELEMENT_STRUCT[] elements);

    [DllImport("libhluart.dylib")]
    public static extern void write_buffer(byte[] buffer, uint length, ELEMENT_STRUCT[] elements);

    [DllImport("libhluart.dylib")]
    public static extern short get_elements_length(byte[] buffer, short size);

    private static uint __data(byte[] temp)
    {
        byte[] res = new byte[4];
        for (int i = 0; i < temp.Length; i++)
        {
            res[i] = temp[i];
        }
        return BitConverter.ToUInt32(res);
    }
    public static ELEMENT_STRUCT GetElement(object val)
    {
        //Debug.Log(val.GetType().Name);
        UInt32 data = 0;
        Element_T t=Element_T.T_UINT32;
        switch (val.GetType().Name)
        {
            case "Byte": data=__data(BitConverter.GetBytes((Byte)val));t = Element_T.T_UINT8; break;
            case "UInt16": data = __data(BitConverter.GetBytes((UInt16)val)); t = Element_T.T_UINT16; break;
            case "UInt32": data = __data(BitConverter.GetBytes((UInt32)val)); t = Element_T.T_UINT32; break;
            case "Single": data = __data(BitConverter.GetBytes((Single)val)); t = Element_T.T_FLOAT; break;
            case "Int16": data = __data(BitConverter.GetBytes((Int16)val)); t = Element_T.T_INT16; break;
            case "Int32": data = __data(BitConverter.GetBytes((Int32)val)); t = Element_T.T_INT32; break;
            default:break;
        }
        
        return new ELEMENT_STRUCT(t,data);
    }
}
